#!/usr/bin/python
# -*- coding: UTF-8 -*-

from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.utils import password_generator


class BMC_Redfish_09(Case):
    """
        Name:
            BMC_Redfish_09
        Label:
            检查指定机箱散热资源信息
        PreCondition:
            1、BMC正常运行
        TestStep:
            1、发送redfish请求，查看Thermal信息，有结果A
            2、查看入风口、出风口、电源温度，有结果B
            3、查看风扇信息，有结果C
        ExpectedResult:
            A）Redfish可以正常显示Thermal信息
            B）入风口、出风口、电源温度显示正确
            C）风扇数量、名字、型号、PN、转速信息显示正确
        Parameters:

        Remark:

    """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.os_host = self.device_dut.get_host(host_type=HostType.HostOS)
        self.redfish_api = self.device_dut.get_api(ns_name="Redfish")

        # 测试步骤
    def procedure(self):
        self.log_step(" 1、发送请求，检查Thermal信息")
        result = self.redfish_api.redfish_chassis_query_chassis_collection()
        chassis_member = result['Members']
        if len(chassis_member) > 1:
            raise Exception(f"机箱资源列表元素数量大于1，不适用此脚本")
        chassis_id = chassis_member[0]['@odata.id'].split('/')[-1]
        chassis_info = self.redfish_api.redfish_chassis_query_chassis_id(chassis_id)
        if chassis_info == None:
            raise Exception(f"Redfish检查机箱资源信息失败")
        thermal_url = chassis_info['Thermal']['@odata.id'].split('/')[-1]
        thermal_info = self.redfish_api.redfish_chassis_query_chassis_id(f"{chassis_id}/{thermal_url}")
        self.log_info(f"Thermal信息为：{thermal_info}")
        self.log_step(" 2、查看入风口、出风口、电源等温度")
        temp_list = thermal_info['Temperatures']
        for i in range(len(temp_list)):
            self.log_info(f"{temp_list[i]['Name']}的温度为：{temp_list[i]['ReadingCelsius']}")
        self.log_step("3、查看风扇信息")
        fans_list = thermal_info['Fans']
        fans_num = len(fans_list)
        self.log_info(f"风扇的数量为：{fans_num}")
        for i in range(fans_num):
            self.log_info(f"风扇名称：{fans_list[i]['Name']}, 型号：{fans_list[i]['Model']}, \
            PN: {fans_list[i]['PartNumber']}, 转速：{fans_list[i]['Reading']}")

    # 测试结束后恢复环境
    def post_test_case(self):
        pass
